package Net.order;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import FileStatus.MD5Filter;
import Util.CryptoUtil;
import baidupcs.api.BaiduPcs;
import baidupcs.auth.AccessToken;

import com.alibaba.fastjson.JSON;

public class PostOrder {

    private BaiduPcs pcs;
    private final static Logger logger = LogManager.getLogger(PostOrder.class.getName());

    public boolean uploadFile(String Mac) {
        File tmp = new File(Mac + "/Order");
        File[] tmp2 = tmp.listFiles();
        if (tmp2 == null || tmp2.length == 0)
            return false;
        tmp = tmp2[0];

        logger.info("send newOrder ,Post 开始工作");

        Path path;
        try {
            path = Paths.get(tmp.getCanonicalPath());
            byte[] context = Files.readAllBytes(path);
            
            pcs.upload(Mac + "/Orders/" + tmp.getName(), CryptoUtil.getEncode(context));
            
            System.gc();
            tmp.delete();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
        
        return true;
    }

    public PostOrder(AccessToken ac) {
        // TODO Auto-generated constructor stub
        this.pcs = new BaiduPcs(ac);
    }

}